<?php
class Server {
  public static function get_obj($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res = $orm->get('pb_colg_class', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($res) > 0) {
      $opts = new stdClass();
      $opts->cd = 't.class_id = :v1';
      $opts->cdv = array(':v1' => $res[0]->id);
      $res['__data'] = $orm->get('pb_colg_rsltlog', 't.date', 'DESC', NULL, NULL, $opts)->d;
    }
    
    echo json_encode($res);
  }
  
  public static function get_rslt($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $res = new stdClass();
    $res->date = $req->date;
    
    $opts = new stdClass();
    $opts->cd = 't.class_id = :v1 AND t.start <= :v2 AND t.end > :v2';
    $opts->cdv = array(':v1' => $req->class_id, ':v2' => $req->date);
    $opts->jt = 'LEFT JOIN pb_colg_student AS t1 ON t.student_id = t1.id';
    $opts->sel = 't.*, t1.id_number AS __id';
    $opts->gb = 't.student_id';
    $res->students = $orm->get('pb_colg_enrolled_class', 't1.id_number', 'ASC', NULL, NULL, $opts)->d;
    
    $opts = new stdClass();
    $opts->cd = 't.id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $res->logs = $orm->get('pb_colg_rsltlog', NULL, NULL, NULL, NULL, $opts)->d;
    if (count($res->logs) > 0) {
      $res->logs[0]->header__ = json_decode($res->logs[0]->header__);
      $opts = new stdClass();
      $opts->cd = 't.rsltlog_id = :v1';
      $opts->cdv = array(':v1' => $res->logs[0]->id);
      $res->rslts = $orm->get('pb_colg_rslt', NULL, NULL, NULL, NULL, $opts)->d;
    } else {
      $obj = new stdClass();
      $obj->class_id = $req->class_id;
      $obj->date = '0000-00-00';
      $obj->header__ = array(array('track_id' => Utils::unique(), 'name' => 'Listening', 'total' => 100, 'ratio' => 25), array('track_id' => Utils::unique(), 'name' => 'Speaking', 'total' => 100, 'ratio' => 25), array('track_id' => Utils::unique(), 'name' => 'Writing', 'total' => 100, 'ratio' => 25), array('track_id' => Utils::unique(), 'name' => 'Reading', 'total' => 100, 'ratio' => 25));
      $obj->add_at = date('Y-m-d', time());
      $res->logs = array($obj);
      $res->rslts = array();
    }
    echo json_encode($res);
  }
  
  public static function rslt($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $req->logs[0]->header__ = json_encode($req->logs[0]->header__);
    if (isset($req->logs[0]->id) && $req->logs[0]->id != 0) {
      $orm->update('pb_colg_rsltlog', $req->logs[0]);
    } else {
      $req->logs[0]->date = $req->date;
      $orm->add('pb_colg_rsltlog', $req->logs[0]);
      $req->logs[0]->id = $orm->last_id();
    }
    
    foreach ($req->rslts as $itm) {
      if (isset($itm->id) && $itm->id != 0 && isset($itm->__c) && $itm->__c == 1) {
        $orm->update('pb_colg_rslt', $itm);
      } else {
        $itm->rsltlog_id = $req->logs[0]->id;
        $orm->add('pb_colg_rslt', $itm);
      }
    }
  }
  
  public static function delete_rslt($orm, $sys, $inst, $is_iframe) {
    $req = json_decode(Utils::get_param('req', '[]'));
    
    $opts = new stdClass();
    $opts->cd = 'rsltlog_id = :v1';
    $opts->cdv = array(':v1' => $req->id);
    $orm->delete_by_opts('pb_colg_rslt', $opts);
    
    $orm->delete('pb_colg_rsltlog', $req);
  }
}
?>